package cn.ljz.user.service;

import cn.ljz.pojo.User;
import cn.ljz.service.UserService;
import cn.ljz.web.util.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Title cloud-userservice.UserServiceImpl
 * @Author LJZ
 * @Description: <TODO description class purpose>
 * @Create 2024/3/13 13:39
 * @Version 1.0
 **/
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Value("${sp.user-service.users}")
    private String userJson;

    @Override
    public User getUser(Integer id) {
        log.info("users json string : " + userJson);
        List<User> list = JsonUtil.from(userJson, new TypeReference<List<User>>() {
        });
        for (User user : list) {
            if (user.getId().equals(id)) {
                return user;
            }
        }
        return new User(id, "name-" + id, "pwd-" + id);
    }

    @Override
    public void addScore(Integer id, Integer score) {
        //喱度添加积分
        log.info("user " + id + " - 增加积分" + score);
    }
}
